package com.scpii.universal.pns.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.doone.comm.paginaction.Pagination;
import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.ClientMoreDto;
import com.scpii.universal.pns.dto.PageCircleDto;
import com.scpii.universal.pns.entity.Comments;
import com.scpii.universal.pns.entity.Resources;
import com.scpii.universal.pns.service.CircleService;
import com.scpii.universal.pns.service.ResourcesService;

@Controller
@Scope("prototype")
public class CircleManageAction extends BaseAction {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -21285393538867068L;

	@Resource
	private CircleService circleService;

	@Resource
	private ResourcesService resourcesService;

	private Pagination pageCircle = new Pagination();
	private Pagination pageComment = new Pagination();
	private Pagination pageSummary = new Pagination();
	private Pagination pagePhoto = new Pagination();
	private List<Comments> reslist;
	private List<PageCircleDto> pageCirList;
	private Integer typeFlag;
	private String tarId;
	private String resId;
	private String intPhotoId;
	private List<Resources> resourcesinfo;

	public String getCircleInit() {

		if (getSelectPerPage("pageCircle") == -1) {
			pageCircle.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			pageCircle.setPageRecord(getSelectPerPage("pageCircle"));
		}
		// 判断是否是第一次查询
		if (getQueryFlag("pageCircle") == -1) {
			pageCircle.setTotalRecord(circleService
					.getCircleCateInitCount(getUserInfo().getAppId()));
		} else {
			pageCircle.setTotalRecord(getQueryFlag("pageCircle"));
		}
		setPageParameters(pageCircle, "pageCircle");
		List<ClientMoreDto> clentmoreList = circleService.getCircleCateInit(
				getUserInfo().getAppId(), pageCircle.getCurrentPage(),
				pageCircle.getPageRecord());

		if (clentmoreList != null && clentmoreList.size() > 0) {
			PageCircleDto circleDto = null;
			pageCirList = new ArrayList<PageCircleDto>();
			for (ClientMoreDto moreDto : clentmoreList) {
				circleDto = new PageCircleDto();
				circleDto.setCateAds(moreDto.getVAR_TITLE_NAME());
				circleDto.setCateName(moreDto.getVAR_MORE_NAME());
				circleDto.setMoreContentId(moreDto.getINT_MORE_CONTENT());
				circleDto.setCount(circleService.getCircleInitCount(
						getUserInfo().getAppId(), Long.parseLong(moreDto
								.getINT_MORE_CONTENT().toString())));
				pageCirList.add(circleDto);
			}
		}
		return SUCCESS;
	}

	public String getCircleManageInit() {
		// 获取以前的messge信息
		typeFlag = 0;
		if (getSelectPerPage("pageSummary") == -1) {
			pageSummary.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			pageSummary.setPageRecord(getSelectPerPage("pageSummary"));
		}
		// 判断是否是第一次查询
		if (getQueryFlag("page") == -1) {
			pageSummary.setTotalRecord(circleService.getCircleInitCount(
					getUserInfo().getAppId(), Long.parseLong(tarId)));
		} else {
			pageSummary.setTotalRecord(getQueryFlag("pageSummary"));
		}
		setPageParameters(pageSummary, "pageSummary");
		reslist = circleService.getCircleInit(getUserInfo().getAppId(),
				Long.parseLong(tarId), pageSummary.getCurrentPage(),
				pageSummary.getPageRecord());
		return SUCCESS;

	}

	public String getCircleInitByResId() {
		// 获取以前的messge信息
		typeFlag = 1;
		if (getSelectPerPage("pageComment") == -1) {
			pageComment.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			pageComment.setPageRecord(getSelectPerPage("pageComment"));
		}
		// 判断是否是第一次查询
		if (getQueryFlag("pageComment") == -1) {
			pageComment.setTotalRecord(circleService
					.getCircleInitByResIdCount(Long.parseLong(resId)));
		} else {
			pageComment.setTotalRecord(getQueryFlag("pageComment"));
		}
		setPageParameters(pageComment, "pageComment");
		reslist = circleService.getCircleByResIdInit(Long.parseLong(resId),
				pageComment.getCurrentPage(), pageComment.getPageRecord());
		return SUCCESS;

	}

	/**
	 * 获取相册图集
	 * 
	 * @return
	 */
	public String getCirclePhotoInfo() {

		if (getSelectPerPage("pagePhoto") == -1) {
			pagePhoto.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			pagePhoto.setPageRecord(getSelectPerPage("pagePhoto"));
		}
		// 判断是否是第一次查询
		if (getQueryFlag("pagePhoto") == -1) {
			pagePhoto.setTotalRecord(resourcesService.getPhotoInfoCount(Long
					.parseLong(resId)));
		} else {
			pagePhoto.setTotalRecord(getQueryFlag("pagePhoto"));
		}
		setPageParameters(pagePhoto, "pagePhoto");
		resourcesinfo = resourcesService.getPhotoInfo(Long.parseLong(resId),
				pagePhoto.getCurrentPage(), pagePhoto.getPageRecord());

		return SUCCESS;
	}

	public String deleteByResId() {
		circleService.updateByResId(Long.parseLong(resId));
		try {
			response.getWriter().print(1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}

	public String deleteByPhotoId() {

		try {
			resourcesService.updateResById(Long.parseLong(intPhotoId));
			response.getWriter().print(1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}

	public Pagination getPageCircle() {
		return pageCircle;
	}

	public void setPageCircle(Pagination pageCircle) {
		this.pageCircle = pageCircle;
	}

	public Pagination getPageComment() {
		return pageComment;
	}

	public void setPageComment(Pagination pageComment) {
		this.pageComment = pageComment;
	}

	public Pagination getPageSummary() {
		return pageSummary;
	}

	public void setPageSummary(Pagination pageSummary) {
		this.pageSummary = pageSummary;
	}

	public List<Comments> getReslist() {
		return reslist;
	}

	public void setReslist(List<Comments> reslist) {
		this.reslist = reslist;
	}

	public Integer getTypeFlag() {
		return typeFlag;
	}

	public void setTypeFlag(Integer typeFlag) {
		this.typeFlag = typeFlag;
	}

	public String getResId() {
		return resId;
	}

	public void setResId(String resId) {
		if (resId != null && !"".equals(resId)) {
			String[] names = resId.split(",");
			if (names != null && names.length > 0) {
				resId = names[0];
			}
		}
		this.resId = resId;
	}

	public List<PageCircleDto> getPageCirList() {
		return pageCirList;
	}

	public void setPageCirList(List<PageCircleDto> pageCirList) {
		this.pageCirList = pageCirList;
	}

	public String getTarId() {
		return tarId;
	}

	public void setTarId(String tarId) {
		if (tarId != null && !"".equals(tarId)) {
			String[] names = tarId.split(",");
			if (names != null && names.length > 0) {
				tarId = names[0];
			}
		}
		this.tarId = tarId;
	}

	public Pagination getPagePhoto() {
		return pagePhoto;
	}

	public void setPagePhoto(Pagination pagePhoto) {
		this.pagePhoto = pagePhoto;
	}

	public List<Resources> getResourcesinfo() {
		return resourcesinfo;
	}

	public void setResourcesinfo(List<Resources> resourcesinfo) {
		this.resourcesinfo = resourcesinfo;
	}

	public String getIntPhotoId() {
		return intPhotoId;
	}

	public void setIntPhotoId(String intPhotoId) {
		this.intPhotoId = intPhotoId;
	}

}
